package com.zjh.workflow.flowable.controller;


import com.zjh.common.entity.Response;
import com.zjh.workflow.flowable.entity.DO.ProcessNodeVariableConfig;
import com.zjh.workflow.flowable.service.ProcessVariableService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 流程节点变量
 */
@RestController
@RequestMapping("/WorkFlow/variable")
public class ProcessVariableController {

    @Resource
    private ProcessVariableService processVariableService;

    /**
     * 根据流程定义Key, 获取流程变量定义信息
     *
     * @param processKey
     * @return
     */
    @GetMapping("getAll")
    public Response<?> getAllVariable(@RequestParam("processKey") String processKey) {
        Map<String, String> res = processVariableService.extractAllVariablesFromBpmn(processKey);
        return Response.success(res);

    }

    @GetMapping("getProcessGateWayVariable")
    public Response<?> getProcessGateWayVariable(@RequestParam("processKey") String processKey) {
        List<ProcessNodeVariableConfig> processVariable = processVariableService.getProcessGateWayVariable(processKey);
        return Response.success(processVariable);

    }

    /**
     * 根据流程定义Key, 获取流程 用户节点 变量定义信息
     *
     * @param processKey
     * @return
     */
    @GetMapping("getProcessAssignVariable")
    public Response<?> getProcessAssignVariable(@RequestParam("processKey") String processKey, @RequestParam(value = "skipFirst", required = false, defaultValue = "true") Boolean skipFirst) {
        List<ProcessNodeVariableConfig> processVariable = processVariableService.getProcessAssignVariable(processKey, skipFirst);
        return Response.success(processVariable);

    }

    /**
     * 根据流程定义Key, 获取第一个 用户节点 流程变量定义信息
     *
     * @param processKey
     * @return
     */
    @GetMapping("getProcessFirstAssignVariable")
    public Response<?> getProcessFirstAssignVariable(@RequestParam("processKey") String processKey) {
        Map<String, String> processFirstAssignVariable = processVariableService.getProcessFirstAssignVariable(processKey);
        return Response.success(processFirstAssignVariable);

    }

    @PostMapping("saveOrUpdateProcessAssignVariable")
    public Response<?> saveOrUpdateProcessAssignVariable(@RequestBody ProcessNodeVariableConfig config) {
        processVariableService.saveOrUpdateProcessAssignVariable(config);
        return Response.success();
    }

    @PostMapping("saveOrUpdateBatchProcessAssignVariable")
    public Response<?> saveOrUpdateBatchProcessAssignVariable(@RequestBody List<ProcessNodeVariableConfig> configs) {
        processVariableService.saveOrUpdateBatchProcessAssignVariable(configs);
        return Response.success();
    }

}
